//陀螺仪相关


#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
Adafruit_MPU6050 mpu;
#define MPU6050_ADDRESS 0x68
#define GYRO_CONFIG 0x1B
#define INT_PIN 5
int16_t gyro_x_threshold = 500;  // x轴角速度阈值
void mpu_init() {
  mpu.begin();
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
  mpu.setMotionDetectionThreshold(1);
  mpu.setMotionDetectionDuration(20);
  mpu.setInterruptPinLatch(false);
  mpu.setInterruptPinPolarity(true);
  mpu.setMotionInterrupt(true);//是否开启运动检测触发中断
  mpu.setMotionDetectionThreshold(0x45);//触发阈值
  pinMode(INT_PIN, INPUT_PULLUP);
}
int mpu_check(){//陀螺仪触发检测
    if (mpu.getMotionInterruptStatus()) {
      sensors_event_t a, g, temp;
      int tick6050 = 0;
      int state6050 = 0;
      while (tick6050 < 20) {
        mpu_init();
        mpu.getEvent(&a, &g, &temp);
        if (!mpu.getMotionInterruptStatus() && abs(abs(g.gyro.z) - 1) < 1.5 && (abs(g.gyro.x) < 1.5 && abs(g.gyro.y) < 2)) {
          state6050 = 1;
          break;
        } else {
         break;
        }
        tick6050++;
        delay(10);
      }
      if (state6050 == 1) {
        mpu_init();
        Wire.end();
        SPI.begin(7, 8, 8, 6);
        //u8g2Fonts.drawUTF8(0, 80, "kkkk");  //display.nextPage();
        return 1;

      } else  {
       mpu_init();
        Wire.end();
        SPI.begin(7, 8, 8, 6);
        return 0;
      }
    } else {
      return 0;
    }
}